package paint;

import java.awt.Toolkit.*;
import java.awt.Image.*;
import java.util.*;
import java.awt.datatransfer.Clipboard.*;
import java.awt.*;
import java.awt.Toolkit.*;
import java.awt.image.*;

/**
 * The user can get the edges of the image. There are no OS/Hardware
 * dependencies and no variances. There is no need for any security constraints
 * and no references to external specifications.
 * 
 * @author  Paint
 * @version 2.0
 */
public class edge extends javax.swing.JDialog {

    /**
     * Creates new form edge.
     * 
     * This also initializes all the components and calls the parent's default
     * constructor. There are no OS dependencies and variances. No security
     * constraints or external specifications.
     * 
     * @param parent
     *            This parameter sends which frame is the parent.
     * @param modal
     *            This parameter could be a model, true or false.
     */
    public edge(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     * 
     * This method initializes the entire form with buttons and new dialoge
     * boxes, etc. It also sets up constraints. There are no OS dependencies and
     * variances. No security constraints or external specifications.
     */
    public void initComponents() {

        // ming 4.24
        if (((Paint) this.getParent()).toolSelectall.getSelected() || ((Paint) this.getParent()).toolSelect.getSelected() || ((Paint) this.getParent()).toolMagicSelect.getSelected()) {
            int cur_layer = ((Paint) this.getParent()).center.currentLayer;
            LinkedList cur_action_list = (LinkedList) (((Paint) this.getParent()).action_list.get(cur_layer));
            LinkedList cur_redo_list = (LinkedList) (((Paint) this.getParent()).redo_list.get(cur_layer));

            int temp1 = cur_redo_list.size();
            for (int i = 0; i < temp1; i++) {
                cur_redo_list.removeLast();
            }
            cur_action_list.add("Region edge detection");
            BufferedImage selected;
            if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelect) {
                selected = ((Paint) this.getParent()).toolSelect.getCopyImage(((Paint) this.getParent()).center);
            } else if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolMagicSelect) {
                selected = ((Paint) this.getParent()).toolMagicSelect.getCopyImage(((Paint) this.getParent()).center);
            } else {
                selected = ((Paint) this.getParent()).toolSelectall.getCopyImage(((Paint) this.getParent()).center);
            }
            Kernel kernel = new Kernel(3, 3, new float[]{-1, -1, -1, -1, 8,
                        -1, -1, -1, -1
                    });
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
                    hints);

            selected = op.filter(selected, null);
            if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelect) {
                ((Paint) this.getParent()).toolSelect.setPastedImage(
                        selected, ((Paint) this.getParent()).center,
                        ((Paint) this.getParent()).toolSelect.getStartX(),
                        ((Paint) this.getParent()).toolSelect.getStartY());
            //	  ((Paint)this.getParent()).toolSelect.backupImage =
            // ((Paint)this.getParent()).center.getBufferedImage();
            } else if (((Paint) this.getParent()).currentTool == ((Paint) this.getParent()).toolSelectall) {
                ((Paint) this.getParent()).toolSelectall.setPastedImage(
                        selected, ((Paint) this.getParent()).center,
                        ((Paint) this.getParent()).toolSelectall.getStartX(),
                        ((Paint) this.getParent()).toolSelectall.getStartY());
            //	  ((Paint)this.getParent()).toolSelectall.backupImage =
            // ((Paint)this.getParent()).center.getBufferedImage();
            }
            //		  else if (((Paint)this.getParent()).currentTool ==
            // ((Paint)this.getParent()).magicSelectTool)
            //		  ((Paint)this.getParent()).magicSelectTool.setPastedImage(selected,
            // ((Paint)this.getParent()).center,
            // ((Paint)this.getParent()).magicSelectTool.getStartX(),
            // ((Paint)this.getParent()).magicSelectTool.getStartY());
            selected.flush();
        } else {
            int cur_layer = ((Paint) this.getParent()).center.currentLayer;
            LinkedList cur_action_list = (LinkedList) (((Paint) this.getParent()).action_list.get(cur_layer));
            LinkedList cur_redo_list = (LinkedList) (((Paint) this.getParent()).redo_list.get(cur_layer));

            int temp1 = cur_redo_list.size();
            for (int i = 0; i < temp1; i++) {
                cur_redo_list.removeLast();
            }
            cur_action_list.add("Image edge detection");
            // ming 4.24 end
            BufferedImage im = ((Paint) this.getParent()).center.getBufferedImage();

            //	  int x = Integer.parseInt(edge_value.getText());

            Kernel kernel = new Kernel(3, 3, new float[]{-1, -1, -1, -1, 8,
                        -1, -1, -1, -1
                    });
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_RENDERING,
                    RenderingHints.VALUE_RENDER_QUALITY);
            BufferedImageOp op = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
                    hints);

            // for (int i=0; i<x; i++)
            im = op.filter(im, null);

            Graphics2D gd = im.createGraphics();

            gd.drawImage(im, 0, 0, null);
            ((Paint) this.getParent()).center.setBufferedImage(im);
            im.flush();
        // ming 4.24
        }
        // ming 4.24 end
        // ming 4.26

        // ming 4.26 end
        ((Paint) this.getParent()).updateUndoList();
    }

    /**
     * This method executes if the event is a CANCEL button, then it closes the
     * dialog. There are no OS dependencies and variances. No security
     * constraints or external specifications.
     * 
     * @param evt
     *            The event of a mouse action.
     */
    /*
     * public void cancelActionPerformed(java.awt.event.ActionEvent evt) {
     * this.closeDialog(new java.awt.event.WindowEvent(this, 0)); }
     * 
     * /** This method executes if the event is a OK button. Then it executes
     * the performed action. In this case, it will edge the picture accordingly.
     * If user selected skew instead of edge, the picture will basically just
     * expand without the borders of the picture to expand.
     * 
     * There are no state transitions and no security constraints or OS
     * dependencies.
     * 
     * @param evt The event of a mouse action.
     */
    /*
     * public void okActionPerformed(java.awt.event.ActionEvent evt) {
     * BufferedImage im =
     * ((Paint)this.getParent()).center.getBufferedImage();
     * 
     * int x = Integer.parseInt(edge_value.getText());
     * 
     * Kernel kernel = new Kernel(3, 3, new float[] { -1, -1, -1, -1, 9, -1, -1,
     * -1, -1}); BufferedImageOp op = new ConvolveOp(kernel);
     * 
     * 
     * for (int i=0; i <x; i++) im = op.filter(im, null);
     * 
     * 
     * Graphics2D gd = im.createGraphics();
     * 
     * gd.drawImage(im,0,0,null);
     * ((Paint)this.getParent()).center.setBufferedImage(im); int temp1 =
     * ((Paint)this.getParent()).redo_list.size(); for (int i=0; i <temp1;
     * i++) ((Paint)this.getParent()).redo_list.removeLast();
     * ((Paint)this.getParent()).action_list.add("edge");
     * ((Paint)this.getParent()).updateUndoList();
     * 
     * this.closeDialog(new java.awt.event.WindowEvent(this, 0)); // ok_action =
     * true; }
     * 
     * 
     * /** This method is for skewing vertically. There are no OS dependencies
     * and variances. No security constraints or external specifications. @param
     * evt The event of an action.
     *//*
     * public void edgeActionPerformed(java.awt.event.ActionEvent evt) { //
     * Add your handling code here: }
     * 
     * 
     * /** Closes the dialog. There are no OS dependencies and variances. No
     * security constraints or external specifications. @param evt The event
     * of an action.
     *//*
     * public void closeDialog(java.awt.event.WindowEvent evt) {
     * setVisible(false); dispose(); }
     */


    /**
     * This method basically creates a new edge object and applies the image
     * into it. If there are arguments, it will take them as well. It will then
     * display the results onto the main canvas. There are no OS dependencies
     * and variances. No security constraints or external specifications.
     * 
     * @param args
     *            The command line arguments
     */
    public static void main(String args[]) {
        new edge(new javax.swing.JFrame(), true).show();
    }

    // Variables declaration - do not modify
    /**
     * Java swing GUI depicting the OK and CANCEL panel.
     */
    private javax.swing.JPanel ok_cancel;
    /**
     * Java swing GUI depicting the OK button.
     */
    private javax.swing.JButton ok;
    /**
     * Java swing GUI depicting the CANCEL button.
     */
    private javax.swing.JButton cancel;
    /**
     * Java swing GUI depicting the CENTER button.
     */
    private javax.swing.JPanel center;
    /**
     * Java swing GUI depicting the edge button.
     */
    private javax.swing.JPanel edge_panel;
    /**
     * Java swing GUI depicting the textfield of the edge change ratio value.
     */
    private javax.swing.JTextField edge_value;
    /**
     * Java swing GUI depicting the Label 4.
     *  
     */
    private javax.swing.JLabel jLabel4;
    /**
     * Java swing GUI depicting the Label 7.
     *  
     */
    private javax.swing.JLabel jLabel7;

    // End of variables declaration
    /**
     * The dialog is closed by OK
     */
    // public boolean ok_action;
}